package com.bank.util;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.bank.code.RespCode;
import com.bank.dao.BaseDao;
import com.bank.dao.TellerDao;
import com.bank.entity.Subject;
import com.bank.excption.CommonException;

public class CommonUtil {
	
		
	public static  String  getWorkDate(){
		BaseDao baseDao = (BaseDao) SpringContextUtils.getBeanById("baseDao");
		return baseDao.getWorkDate();
	}
	public static  Subject  getSubjectInfo(String subject_number){
		BaseDao baseDao = (BaseDao) SpringContextUtils.getBeanById("baseDao");
		return baseDao.getSubjectByNumer(subject_number);
	}
	
	public static String getCurrentTime(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String   currentTime = df.format(new Date());
		return currentTime;
	}
	/**
	 * 获取柜员流水号
	 * @param tellerId 代号 
	 * @param transOri 交易机构
	 * @param workdate 工作日期
	 * @return 柜员流水号
	 * */
	@Transactional
	public static String getTellerSerno(String tellerId, String  transOri, String workdate){
		Logger logger = Logger.getLogger(CommonUtil.class);
		TellerDao tellerDao = (TellerDao)SpringContextUtils.getBeanById("tellerDao");
		if(tellerDao != null){
			logger.debug("userDao获取失败");
		}
		String serialNumber = tellerDao.getTellerSerno(tellerId);
		String tellerSerialNumber = workdate+tellerId+serialNumber;   //流水号 = 交易日期+柜员代号+ 序号
		return tellerSerialNumber;
	}
	
	/**
	 * 密码加密
	 * @param  待加密密码
	 * @return 密文
	 * @throws CommonException 
	 * 
	 * */
	public static String passwordEncryption(String password) throws CommonException
	{
		MessageDigest md5;
		String ciphertext = null;
		try {
				md5 = MessageDigest.getInstance("SHA-256");
				byte[] srcBytes = password.getBytes();  
				md5.update(srcBytes);  
				ciphertext = new String(md5.digest());  
				return ciphertext;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new CommonException(RespCode.PASSWORD_ENCRYPT_FAIL, "加密失败");
		}  
      	}	

}
